/*
 * Copyright 2011 Michael Guo
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uc.cs.guome.ir;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Generate a list of document objects from input files
 * @author(name="Michael Guo",date="01/18/2011")
 * @version 0.1
 */
public class DocumentGenerator {
	
	private ArrayList<String> inputFiles;
	private StopWordList stopWords;
	private HashMap<Integer, Document> docs;
	
	public DocumentGenerator(ArrayList<String> inputs, String swfname) {
		this.setInputFiles(inputs);
		this.stopWords = new StopWordList(swfname);
		docs = new HashMap<Integer, Document>();
	}
	
	public HashMap<Integer, Document> generate() throws Exception {
		
		if (inputFiles == null|inputFiles.isEmpty()){
			System.out.println("Input File Error");
		}
		
		docs = new HashMap<Integer, Document>();
		
		Iterator<String> ir = inputFiles.iterator();
		while(ir.hasNext()) {
			FileInputStream fis = new FileInputStream(ir.next());
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			
			String line = null;
			while((line=br.readLine())!=null) {
				Document doc = Parser.parse(line, stopWords);
				docs.put(doc.getDocID(), doc);
			}
			dis.close();
		}
		
		return this.docs;
	}
	
	public StopWordList getStopWords() {
		return stopWords;
	}

	public void setStopWords(StopWordList stopWords) {
		this.stopWords = stopWords;
	}

	public void setInputFiles(ArrayList<String> inputFiles) {
		this.inputFiles = inputFiles;
	}

	public ArrayList<String> getInputFiles() {
		return inputFiles;
	}
}
